/**
 * this class will implement the controller dealing with all actions on/with subscriptions
 * 
 * @author Gil Levinson
 * @author Boris Fradlis
 * @author Yogev Levi
 * @author Hila Porat
 *  
 */
package control;

import java.io.Serializable;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;

// Start of SubscriptionController ********************************************************

public class SubscriptionController extends AbstractController 
{
	// class variables ********************************************************************
	
		private SystemTypes sysType;
		private boolean returned = false;
		
		// Constructor ************************************************************
		
		public SubscriptionController(SystemTypes myType)
		{
			this.sysType=myType;
			returned=false;
		}
		
		// Controller methods *********************************************************
		
		/**
		 * this method will create new Order for all type of client
		 * @param sysType - will receive the request for the new order 
		 * @return Post if the booking was successful or not
		 */
		public SystemTypes createNewSubscription(SystemTypes myType)
		{
			returned=false;		
			myType.setSysMode(SystemMode.NEW_SUBSCRIPTION);
			SendToServer(myType);
			while(!returned){}
			System.out.println("Got here 0");
			return sysType;
		}
		
		/**
		 * this method will renew an existing subscription.
		 * @param sysType - will receive the renewal request.
		 * @return return the renewal result.
		 */
		public SystemTypes renewSubscription(SystemTypes myType)
		{
			returned=false;		
			myType.setSysMode(SystemMode.RENEW_SUBSCRIPTION);
			SendToServer(myType);
			while(!returned){}
			return sysType;
		}
		
		@Override
		public boolean SendToServer(Serializable obj)
		{
			VCP.client.send(obj);
			sysType = (SystemTypes) VCP.client.getResult();
			if (sysType.getSysMode() != SystemMode.OK)
			{
				returned = true;
				return false;
			}
			returned = true;
			return true;		
		}
		
		// Setters and getters ********************************************************


		/**
		 * @return the sysType
		 */
		public SystemTypes getSysType() {
			return sysType;
		}

		/**
		 * @param sysType the sysType to set
		 */
		public void setSysType(SystemTypes sysType) {
			this.sysType = sysType;
		}

		/**
		 * @return the returned
		 */
		public boolean isReturned() {
			return returned;
		}

		/**
		 * @param returned the returned to set
		 */
		public void setReturned(boolean returned) {
			this.returned = returned;
		}

}
